﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;

namespace PortalWeb.Quartz.Server
{
    class Program
    {
        static void Main(string[] args)
        {
            var properties = new NameValueCollection();
            properties["quartz.scheduler.instanceName"] = "RemoteServerSchedulerClient";

            // 设置线程池
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = "5";
            properties["quartz.threadPool.threadPriority"] = "Normal";


            properties["quartz.jobStore.useProperties"] = "true";
            properties["quartz.jobStore.clustered"] = "true";
            properties["quartz.jobStore.misfireThreshold"] = "60000";
            properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
            properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
            properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz";

            properties["quartz.jobStore.dataSource"] = "myDS";
            properties["quartz.dataSource.myDS.connectionString"] =
                @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Source\WebSolution\PortalWeb\PortalWeb\App_Data\DATABASE\QUARTZDB.MDF;Integrated Security=True;Connect Timeout=30";
            properties["quartz.dataSource.myDS.provider"] = "SqlServer-20";

            // 远程输出配置
            properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz";
            properties["quartz.scheduler.exporter.port"] = "556";
            properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler";
            properties["quartz.scheduler.exporter.channelType"] = "tcp";

            var schedulerFactory = new StdSchedulerFactory(properties);
            var scheduler = schedulerFactory.GetScheduler();

            //var job = JobBuilder.Create<PrintMessageJob>()
            //    .WithIdentity("myJob", "group1")
            //    .Build();

            //var trigger = TriggerBuilder.Create()
            //    .WithIdentity("myJobTrigger", "group1")
            //    .WithCronSchedule("/10 * * ? * *")
            //    .Build();
            //scheduler.ScheduleJob(job, trigger);
            scheduler.Start();
        }
    }

    public class PrintMessageJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("Hello!");
        }
    }
}
